<?php
/**
 * Created by PhpStorm.
 * User: River
 * Email: river0428@gmail.com
 * Date: 2018/11/12
 * Time: 下午1:45
 */

namespace Modules\Admin\Main\Controllers;

use App\Http\Controllers\Controller;
use App\Models\OilCard;
use App\Models\RechargeOrder;
use App\Models\User;
use Illuminate\Http\Request;

class OilCardController extends Controller
{
    public function index()
    {
        return view('main::oil_card', \Request::all());
    }

    public function all(Request $request)
    {
        $limit = $request->input('limit', 10);
        $offset = $request->input('offset', 0);
        $field = $request->input('field');
        $value = $request->input('keyword');

        $query = OilCard::query();

        if($field && $value){
            if($field=='card_no'){
                $query = $query->where('card_no',$value);
            }
            if($field=='mobile'){
                $query = $query->where('mobile',$value);
            }
        }


        $data['total'] = $query->count();
        $data['items'] = [];
        $res = $query->take($limit)->skip($offset)->orderBy('id', 'desc')->get();
        foreach ($res as $item) {
            $_item = $item->toArray();
            $_item['mobile'] = convert_mobile($_item['mobile']);
            $_item['current_month_recharged_amount'] = $item->getCurrentMonthRechargedAmount();
            $_item['current_month_amount'] = $item->getCurrentMonthAmount();
            $_item['total_recharged_amount'] = $item->getTotalRechargedAmount();
            // $_item['open_code'] = $item->user->open_code;
            $data['items'][] = $_item;
        }

        return success($data);
    }

    /**
     * 用户油卡列表
     *
     * @author yls
     * @param Request $request
     * @return \Illuminate\Contracts\Routing\ResponseFactory|\Symfony\Component\HttpFoundation\JsonResponse|\Symfony\Component\HttpFoundation\Response
     */
    public function userCard(Request $request)
    {
        $userId = $request->get('user_id', 0);
        if (empty($userId)) {
            return fail(400, '参数错误');
        }

        $orderId = $request->get('order_id', 0);
        $orderInfo = false;
        if (!empty($orderId)) {
            $orderInfo = RechargeOrder::query()->where([
                ['id', '=', $orderId],
                ['user_id', '=', $userId],
            ])->first();
        }

        $query = OilCard::query();
        $res = $query->where('user_id', $userId)->orderBy('created_at', 'desc')->get();

        if (!empty($res)){
            foreach ($res as &$value) {
                if (1 === (int)$value->type) {
                    $value->type_name = '中石化';
                } else {
                    $value->type_name = '中石油';
                }

                if (1 === (int)$value->status) {
                    $value->status_name = '正常';
                } else {
                    $value->status_name = '删除';
                }

                if ($orderInfo && $orderInfo->oil_card_id == $value->id) {
                    $value->is_order_card = 1;
                } else {
                    $value->is_order_card = 0;
                }

                if ($orderInfo && $orderInfo->card_type == $value->type) {
                    $value->is_same_card = 1;
                } else {
                    $value->is_same_card = 0;
                }
            }
        }

        return success($res);
    }
}